import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:tv_friso_screen/sales_page.dart';
import 'package:tv_friso_screen/store_page.dart';
import 'package:tv_friso_screen/utils/phone_info_utils.dart';
import 'package:tv_friso_screen/utils/share_pre_utils.dart';

import 'home_page.dart';

//店主 主导页
class HomeMainPage extends StatefulWidget {
  const HomeMainPage({super.key});

  // final TabsBar tabBar;

  // const HomeMainPage({Key? key, required this.tabBar}) : super(key: key);

  @override
  State<HomeMainPage> createState() => _HomeMainPageState();
}

class _HomeMainPageState extends State<HomeMainPage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  int _currentIndex = 0;

  final List<Widget> _pageList = [
    HomePage(),
    SalesPage(),
    LineChartSample1(),
  ];

  final pageController = PageController();

  Timer? downTimer; //右上角时分秒显示倒计时
  var nowTime = "";

  void countDownTimer() {
    downTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
      //每隔 1 秒钟 刷新一次时分秒
      setState(() {
        nowTime = PhoneInfoUtils.getTime();
      });
    });
  }

  @override
  void initState() {
    super.initState();
    countDownTimer();
  }

  @override
  void dispose() {
    super.dispose();
    downTimer?.cancel();
  }

  //当PageView可滑动时 会调用这个方法
  void onPageChanged(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context); // 必须调用super.build(context)
    return Scaffold(
      body: Stack(
        children: [
          PageView(
            controller: pageController,
            onPageChanged: onPageChanged,
            // physics: const NeverScrollableScrollPhysics(), //禁止滑动
            physics: const AlwaysScrollableScrollPhysics(),
            children: _pageList,
          ),
          Container(
            padding: EdgeInsets.only(top: 20.w, right: 20.w),
            height: 100.w,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                  nowTime,
                  style: TextStyle(
                    color: const Color(0xFFBDE1FF),
                    fontSize: 28.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(
                  width: 20.w,
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      getWeekDay(),
                      style: TextStyle(
                        color: const Color(0xFFBDE1FF),
                        fontSize: 24.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    Text(
                      PhoneInfoUtils.getDate(),
                      style: TextStyle(
                        color: const Color(0xFFBDE1FF),
                        fontSize: 24.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  //获取周几
  String getWeekDay() {
    DateTime now = DateTime.now(); // 获取当前日期和时间

    String weekDay;
    switch (now.weekday) {
      case DateTime.monday:
        weekDay = "星期一";
        break;
      case DateTime.tuesday:
        weekDay = "星期二";
        break;
      case DateTime.wednesday:
        weekDay = "星期三";
        break;
      case DateTime.thursday:
        weekDay = "星期四";
        break;
      case DateTime.friday:
        weekDay = "星期五";
        break;
      case DateTime.saturday:
        weekDay = "星期六";
        break;
      default:
        weekDay = "星期天";
        break;
    }
    return weekDay;
  }
}
